<template>
  <div class="app-content">
    <!-- 顶部tips -->
    <div class="tips" ref="tips">
      <h3>退货流转单</h3>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="订单号">
          <el-input size="small" v-model="SearchForm.OrderNo" placeholder="请输入您要搜索的订单号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="primary" @click="OnSeacrh">查询</el-button>
          <el-button size="mini" type="success" @click="CreateDialog">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 中间表格部分 -->
    <el-table v-if="tabaleHeight.length" ref="table" :data="tableData" border style="width: 100%" :height="tabaleHeight"
      size="mini" v-loading="loading">
      <el-table-column :label="item.title" :width="item.width" show-overflow-tooltip v-for="item in Page.tableHeader"
        :key="item.title">
        <template v-slot="scope">
          <ColumnTable :CurretItem="scope.row" :ItemValue="item.value" />
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="120">
        <template v-slot="scope">
          <el-button size="mini" type="text" :disabled="scope.row.return_status == '' ? false : true"
            @click="ReturnDialog(scope.row, 'ReturnDialog')">退货入库</el-button>
          <el-button size="mini" type="text" @click="UpdataDialog(scope.row, 'UpdataDialog')">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页部分 -->
    <div class="pagination">
      <el-pagination @current-change="handleCurrentChange" :current-page="paramsquery.pageIndex"
        :page-size="paramsquery.pageSize" layout="total, prev, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 全局表单弹窗 -->
    <DialogComponent ref="DialogComponent" :Dialog="Dialog" :Mobile="Mobile" @Submit="Submit" />
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex"; // 引入vuex属性
import mixin from "@/mixins/mixin"; // 引入mixin
import { formattedDate } from "@/utils/index"; // 时间函数
import { GetReturnList } from "@/api/index"; // 退货流转单数据
import { addReturn, UpdateReturn, GetExpressList } from "@/api/index"; // 添加退货流转  修改退货流转 获取快递列表
import { Page } from "./index"; // 引入页面信息
export default {
  mixins: [mixin],
  data() {
    return {
      /* 页面信息 */
      Page,
      /* 搜索部分 */
      SearchForm: {
        OrderNo: "",
      },
      loading: true, // 表格加载
      tableData: [], // 表格数据
      paramsquery: {
        pageIndex: 1,
        pageSize: 30,
      }, // 分页参数
      total: 0, // 总数
      /* 弹窗的信息 默认指定一个 */
      Dialog: Page.Dialog.UpdataDialog,
    };
  },
  computed: {
    ...mapState(["isMobile", "CurrentItem"]),
  },
  created() { },
  mounted() {
    this.GetDataList();
    this.GetOptionsList();
  },
  watch: {},
  methods: {
    ...mapActions(["SetCurrentItem"]),
    // 搜索列表
    OnSeacrh() {
      this.paramsquery.pageIndex = 1;
      this.GetDataList();
    },
    // 获取发货快递
    GetOptionsList() {
      GetExpressList().then((res) => {
        // console.log(res);
        res.map((item) => {
          item.value = String(item.id);
          item.label = item.ship_name;
        });
        this.Page.Dialog.UpdataDialog.DialogFormType[1].options = res;
        this.Page.Dialog.ReturnDialog.DialogFormType[2].options = res;
      });
    },
    // 请求列表数据
    GetDataList() {
      this.loading = true;
      const data = {
        pageIndex: this.paramsquery.pageIndex,
        searchOrderId: this.SearchForm.OrderNo,
      };
      GetReturnList(data).then((res) => {
        // console.log(res);
        this.loading = false;
        res.orderList.map((item) => {
          Object.entries(item).forEach(([key, value]) => {
            if (value === null) {
              item[key] = "";
            }
          });
        });
        this.tableData = res.orderList;
        this.pageIndex = res.pageIndex;
        this.pageSize = res.pageSize;
        this.total = res.pageTotal;
      }).catch(err => {
        console.log(err);
        this.loading = false
      })
    },
    // 新增发货流转单
    CreateDialog() {
      this.Dialog = this.Page.Dialog["CreateDialog"];
      this.$refs.DialogComponent.openModal("CreateDialog");
    },
    // 修改退货入库弹窗
    UpdataDialog(rows, name) {
      const { DialogForm, DialogFormType } = this.Page.Dialog[name];
      DialogFormType.forEach((item) => {
        DialogForm[item.value] = rows[item.value];
      });
      this.Page.Dialog[name].DialogTtile = "修改退货入库";
      this.Page.Dialog[name].DialogConfirmText = "确认修改退货入库";
      this.SetCurrentItem(rows);
      this.Dialog = this.Page.Dialog[name];
      this.$refs.DialogComponent.openModal(name);
    },
    // 退货入库弹窗
    ReturnDialog(rows, name) {
      const { DialogForm, DialogFormType } = this.Page.Dialog[name];
      DialogFormType.forEach((item) => {
        DialogForm[item.value] = rows[item.value];
      });
      this.Page.Dialog[name].DialogTtile = "退货入库";
      this.Page.Dialog[name].DialogConfirmText = "确认退货入库";
      this.SetCurrentItem(rows);
      this.Dialog = this.Page.Dialog[name];
      this.$refs.DialogComponent.openModal(name);
    },
    // 确认提交的弹窗数据
    Submit(e) {
      // console.log(e);
      if (e.DialogTtile == "新增退款退货订单") this.AddOrder(e)
      if (e.DialogTtile === "退货入库")
        this.UpdataInfo(e.form, e.multipleSelection.id);
      if (e.DialogTtile === "修改退货入库") this.UpdataInfo(e.form);
    },
    // 确认新增退货流转单
    AddOrder(e) {
      // console.log(e);
      addReturn({ orderId: e.form.Return_order_id }).then((res) => {
        if (res.msg) {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.GetDataList();
          this.$refs.DialogComponent.closeModal();
        } else {
          this.$message.error("新增失败");
        }
      });
    },
    // 修改退货入库
    UpdataInfo(e, stock_id) {
      // console.log(e);
      const data = {
        id: this.CurrentItem.id,
        stock_id,
        orderId: e.orderId,
        supplier_id: e.supplier_id,
        warehouse_id: e.warehouse_id,
        sku: e.sku,
        sku_name: e.sku_name,
        retrun_sku_num: e.sku_num,
        ship_time: e.ship_time == "" ? null : formattedDate(e.ship_time),
        ship_name: e.ship_name,
        ship_no: e.ship_no,
        return_ship_name: e.return_ship_name,
        return_ship_no: e.return_ship_no,
        return_ship_time:
          e.return_ship_time == "" ? null : formattedDate(e.return_ship_time),
        return_status: e.return_status,
        return_signer: e.return_signer,
        return_pic: e.img || e.ability_pic || e.return_pic,
        return_trans: e.return_trans === "" ? 0 : e.return_trans,
        return_payer: e.return_payer,
        remark: e.remark,
      };
      // console.log(data);
      if (!stock_id) delete data.stock_id;
      // console.log(data);
      Object.entries(data).forEach(([key, value]) => {
        if (value == "") {
          data[key] = null;
        }
      });
      UpdateReturn(data).then((res) => {
        // console.log(res);
        if (res.msg) {
          this.$message({
            message: stock_id ? '退货成功' : "修改成功",
            type: "success",
          });
          this.GetDataList();
          this.$refs.DialogComponent.closeModal();
        } else {
          this.$message.error(stock_id ? '退货失败' : "修改失败");
        }
      });
    },
  },
  components: {},
};
</script>

<style scoped lang="less">
@import url(../../../style/index.css);

/deep/ .el-form-item {
  margin-bottom: 0;
}
</style>
